<template>
	<view class="checkin">
		<logo-title />
		<view class="form-tip">*必须填写（Require）</view>
		<input-box label="姓名(Name)" :required="true" :value.sync="member.name" />
		<input-select label="证件类型 (ID Type)" :required="true" :value.sync="member.cardType" :range="cardTypeList" />
		<input-box label="证件号码(ID Number)" :required="true" :value.sync="member.idnumber" />
		<input-box label="公司名称(Company)" :required="true" :value.sync="member.company" :disabled="iscompany" />
		<input-box label="展位(Booth)" :required="true" :value.sync="member.booth" v-show="iscompany" :disabled="iscompany" />
		<input-box label="公司电话(Company Phone)" :required="false" :value.sync="member.company_phone" type="number" v-show="iscompany" />
		<input-box label="职务(Position)" :required="true" :value.sync="member.position" tip="(请填写具体职位,Please fill in the specific position.)" />
		<input-box label="移动电话(Mobile Phone)" :required="true" :value.sync="member.phone" type="number" />
		
		<view class="public">
			<view class="public_ask">是否愿意公开联系方式，方便采购对接</view>
			<view class="public_ask_en">(Are you willing to make your contact information public for procurement purposes?)</view>
			
			<radio-group v-if="member.public=='是'" @change="switch_public">
				<radio checked="checked" value="是"  color="#eb6100"> 是  </radio> 
				<radio  value="否"   color="#eb6100"> 否 </radio> 
			</radio-group>
			
			 <radio-group v-if="member.public!='是'" @change="switch_public">
				<radio  value="是"   color="#eb6100"> 是 (Yes) </radio> 
				<radio checked="checked" value="否"   color="#eb6100"> 否 (No)</radio> 
			</radio-group>
		 
		</view>
 
		<input-region label="省份/市(Province/City)" :required="true" :value.sync="member.address" v-show="iscompany" />
		<input-box label="详细地址(Address)" :required="false" :value.sync="member.addrDetail" type="text" v-show="iscompany" />
		<input-box label="电子邮箱(Email)" :required="false" :value.sync="member.email" type="text" v-show="iscompany" />
		<!-- <input-checkbox label="行业选择" :required="true" :value.sync="member.industry" type="text" v-show="iscompany" /> -->
		<input-select label="行业选择( Industry Selection)" :required="iscompany" :value.sync="member.industry" :range="industryList" />
		<button class="submit-btn" type="default" @tap="save"> 提交 (SUBMIT) </button>
	</view>
</template>

<script>
	import LogoTitle from '@/components/logo-title/logo-title.vue'
	import InputBox from '@/components/input-box/input-box.vue'
	import InputSelect from '@/components/input-select/input-select.vue'
	import InputRegion from '@/components/input-region/input-region.vue'
	import InputCheckbox from '@/components/input-checkbox/input-checkbox.vue'
	import Config from '@/config.js'
	import { genMid } from '@/utils/util.js'
	export default {
		components: {
			LogoTitle,
			InputSelect,
			InputRegion,
			InputCheckbox,
			InputBox
		},
		data() {
			return {
				iscompany: true,
				cardTypeList: ['身份证 (ID Card)','护照 (Passport)'],
				member: {
					name: '',
					company: '',
					cardType: '',
					idnumber: '',
					company_phone: '',
					company_id: '',
					phone: '',
					openid: '',
					addrDetail: '',
					address: '',
					email: '',
					position: '',
					industry: '',
					public: '',
					newone:'',
					booth: ''
				}, 
				url: '',
				industryList: ['成品制造商 (Finished Goods Manufacturer)', '配件制造商 (Accessories Manufacturer)', '经销商 (Distributor)', '设备安装工程 (Equipment Installation Engineering)', '媒体单位 (Media Organization)', '其他行业 (Other Industries)']
				
			}
		},
		onLoad(option) { 
			//上一个页面过来的时候，必定会带来openid和公司码。我们应该根据这两种情况去判断，客户的来路
			console.log('option: ', option)
			
			
			this.url = Config.url
			var that = this;
			
			
			
			//判断用户是从客户那边过来，还是工作人员那边过来的
			if (this.iscompany) {
				uni.setNavigationBarTitle({
					title: '公司人员参展申请表格'
				})
				
				// 如果是公司人员，会 member 中添加 company_id 字段
				this.member.company_id = option.companyCode;
				this.iscompany = true;  
				
				
			} else {
				uni.setNavigationBarTitle({
					title: '观众参展申请表格 (Form for Visitors)'
				})
			}
			
 
 		
			//获取的openid，用来查找用户的信息。
			if (option.openid) {
			
				let phone = 10000000000
				if(option.phone)
				{
					phone = option.phone
				}
				this.member.openid = option.openid
				this.getByOpenid(option.openid,option.phone)
				
			}
			else
			{
				uni.showToast({
					title:'错误：请关注公众号后从菜单栏进入报名。'
				})
				console.log("获取openid出错")
			}
			


			
			
			



		},
		methods: {
			check2() {

				if (!this.checkStringEmpty(this.member.openid)) {
					uni.showToast({
						title: 'openid不存在',
						icon: 'none',
						duration: 2000
					});
					return false; 
				}
				if (!this.checkStringEmpty(this.member.name)) {
					uni.showToast({
						title: '名称不能为空 (Name is required)',
						icon: 'none',
						duration: 2000
					});
					return false;
				}
				// if (!this.checkCardNo(this.member.idnumber)) {
				// 	uni.showToast({
				// 		title: '请输入正确的身份证号码',
				// 		icon: 'none',
				// 		duration: 2000
				// 	});
				// 	return false;
				// }
				// if (!this.checkMobile(this.member.phone)) {
				// 	uni.showToast({
				// 		title: '请输入正确的手机号码',
				// 		icon: 'none',
				// 		duration: 2000
				// 	});
				// 	return false;
				// }
				if (!this.checkStringEmpty(this.member.position)) {
					uni.showToast({
						title: '职务不能为空 (Position is required)',
						icon: 'none',
						duration: 2000
					});
					return false;
				}

				if (this.iscompany) {
					if (!this.checkStringEmpty(this.member.company)) {
						uni.showToast({
							title: '公司不能为空 (Company is required)',
							icon: 'none',
							duration: 2000
						});
						return false;
					}

					if (!this.checkStringEmpty(this.member.industry)) {
						uni.showToast({
							title: '请选择行业 (Industry is required)',
							icon: 'none',
							duration: 2000
						});
						return false;
					}

					if (!this.checkStringEmpty(this.member.address)) {
						uni.showToast({
							title: '请选择省份/市 (Province/City is required)',
							icon: 'none',
							duration: 2000
						});
						return false;
					}

				}

				return true;
			},
			
			
			
			
			switch_public(e){
				this.member.public=e.detail.value
				console.log("event.detail",e.detail.value)
			},
			
			
			
			
			
			save() {
				if (!this.check2()) {
					return;
				}

				if (this.member.isUpdate != 1) {
					this.member.mid = genMid()
					console.log('mid: ', this.member.mid)
				} 
				
				let post_member=JSON.stringify(this.member, null, 2) 
				console.log(post_member); // 打印整个对象 
				
				uni.request({
					url: this.url + 'add.php',
					data: post_member,
					method: 'POST',
					success: res => {
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 2000
						});
						
						console.log(res.data.msg)
						
						if (res.data.code == '0') {
							uni.redirectTo({
								url: '../qrcode/qrcode?phone=' + this.member.phone + '&openid=' + this
									.member.openid + '&mid=' + this.member.mid + '&iscompany=' + this
									.iscompany + '&companyCode=' + this.member.company_id
							});
						}
					}
				});
			},
			getByOpenid(openid,phone) {
				 
				console.log('getByOpenid 程序执行：'+openid+"_"+phone)
				uni.request({
					url: this.url + 'getMember.php',
					data: {
						"openid": openid,
						"phone": phone
					},
					method: 'POST',
					success: (res) => {
						console.log('获取用户信息', res.data.data) 
						console.log('获取用户信息 code ', res.data.code) 
						console.log('获取用户信息 openid ', res.data.data.openid) 
	 
						
						if (res.data.code == '1') {
 
						    this.member = res.data.data;  
							this.member.public = res.data.public;
							this.member.openid = openid ;
							//this.addressText = res.data.data.['address'];
							//this.index = this.picker.indexOf(res.data.data[0]['industry']);

							// 本次资料是否作为更新，或是插入
							//这个 isUpdate 的作用貌似是为了判断该人员是否已有 mid
							this.member.isUpdate = 1;
							
							//如果是公司，如果是新人空id，如果又是update的话，就设置update2
							 if(!(res.data.data.openid > 1)) 
							 {
								this.member.newone = 1; 
								console.log('this.member.newone', this.member.newone)
							 }
 
							// 检查该 openid 用户是否有 company_id，有则为公司员工
							if (this.checkStringEmpty(this.member.company_id.trim())) {
								this.iscompany = true;
							}
							
						}
						
						
						// 若为工作人员
						if(this.iscompany === true) {
							// 获取该公司编号对应的公司名称和展位
							this.getCompanyInfo(this.member.company_id)
						}
				
				
					}
				});
			},
			
			getCompanyInfo(companyId) {
				// 获取公司信息
				uni.request({
					url: this.url + 'getCompany.php',
					data: {
						companyId: this.member.company_id
					},
					method: 'POST',
					success: res => {
						console.log('获取公司信息:', res)
						if (res.data.code == '0') {
							const {name, booth } = res.data.data[0]
							this.$set(this.member, 'company', name)
							this.$set(this.member, 'booth', booth)
							
 		
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
						}
					}
				})
				
			}
		},


	}
</script>



<style lang="less">
	@import url('@/common/less/vars.less');
	.checkin {
			font-size: @main-font-size;
			box-sizing: border-box;
			padding: 0 51.56rpx;
			padding-bottom: 60px;
	 
	
		}
		
		
		.submit-btn {
			margin-top: 60px;
			width: 100%;
			height: 75rpx;
			line-height: 80rpx;
			background-color: @theme-color;
			color: #fff;
			font-size: 24rpx;
			border-radius: 5px;
			border: none;
		}
		
		
		
		.form-tip {
			color: @theme-color;
			margin-top: 24px;
			margin-bottom: 10px;
		}
		
		
		.public_ask{margin-bottom: 5rpx;}
		.public_ask_en{margin-bottom: 15rpx;font-size: 20rpx;}
		.public{margin-bottom: 35rpx;}
		.public radio{margin-right: 35rpx;}
</style>
